﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.Service.View
{
    /// <summary>
    /// 本周均值
    /// </summary>
    public class ServiceOperatorAvgWeek : Benefit.Interface.View.IOperatorAvgWeek
    {
        Benefit.DB.DBManager db = null;
        public ServiceOperatorAvgWeek(Benefit.DB.DBManager db)
        {
            this.db = db;
        }


        /// <summary>
        /// 初始化交易员周均值
        /// </summary>
        /// <param name="operatorId"></param>
        /// <param name="tradeHistoryId"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public void InitOperatorAvgWeek(int tradeHistoryId)
        {
            Interface.Sys.ITradeHistory ith = new Sys.ServiceTradeHistory(db);
            //Models.Sys.TradeHistory th = new Sys.TradeHistory();
            Models.Sys.TradeHistory th = ith.GetModel(tradeHistoryId);
            Interface.Sys.IDayOperatorAccount idoa = new Sys.ServiceDayOperatorAccount(db);
            //Sys.DayOperatorAccount doa = new Sys.DayOperatorAccount();

            #region 得到本周所有的交易日

            List<Models.Sys.TradeHistory> tradeDays = ith.GetWeekTradeingDay(th.WPdate, th.YPdate);

            #endregion
            Interface.Sys.IOperator iop = new Sys.ServiceOperator(db);
            var query = iop.GetList();


            foreach (Models.Sys.Operator op in query)
            {
                List<Models.Day.AccountDayChange> change = new List<Models.Day.AccountDayChange>();
                List<Models.Day.AccountDayAnalysis> analysis = new List<Models.Day.AccountDayAnalysis>();

                #region 得到每个交易日用户的账户交易情况
                foreach (Models.Sys.TradeHistory h in tradeDays)
                {
                    List<Models.Sys.Account> accounts = idoa.GetDayOperatorAccounts(ith.GetDateTimeFromTradeHistoryId(h.Id), op.Id);
                    foreach (Models.Sys.Account account in accounts)
                    {
                        Interface.Day.IAccountDayChange iadc = new Day.ServiceAccountDayChange(db);
                        //Models.Day.AccountDayChange adc = new Data.Models.AccountDayChange();
                        Models.Day.AccountDayChange adc = iadc.GetAccountDayChange(h.Id, account.Id);
                        change.Add(adc);
                        Interface.Day.IAccountDayAnalysis iada = new Day.ServiceAccountDayAnalysis(db);
                        //Models.Day.AccountDayAnalysis ada = new Models.Day.AccountDayAnalysis();
                        Models.Day.AccountDayAnalysis ada = iada.GetAccountDayAnalysis(h.Id, account.Id);
                        analysis.Add(ada);
                    }
                    Models.View.OperatorAvgWeek oaw = new Models.View.OperatorAvgWeek();
                    var exist = db.OperatorAvgWeek.Where(a => a.Week == th.WPdate).Where(a => a.Year == th.YPdate).ToList();
                    if (exist.Count == 0)
                    {
                        oaw.Year = th.YPdate;
                        oaw.Month = th.MPdate;
                        oaw.Week = th.WPdate;

                        if (change.Count > 0)
                        {
                            oaw.AvgBillBenefit = Math.Round(analysis.Sum(a => a.AvgBillBenefit) / change.Count(), 2);
                            oaw.AvgPostionTime = Math.Round(analysis.Sum(a => a.AvgPostionTime) / change.Count(), 2);
                            oaw.BillCount = Math.Round(change.Sum(a => a.BillCount) / change.Count(), 2);
                            oaw.DayCount = Math.Round(change.Sum(a => a.DayCount) / change.Count(), 2);
                            oaw.Free = Math.Round(change.Sum(a => a.Free) / change.Count(), 2);
                            oaw.LoseCount = Convert.ToInt32(change.Sum(a => a.LoseCount) / change.Count());
                            oaw.MaxReturn = Math.Round(analysis.Sum(a => a.MaxReturn) / change.Count(), 2);
                            oaw.OperatorId = op.Id;
                            oaw.OverStopLoss = Math.Round(analysis.Sum(a => a.OverStopLoss) / change.Count(), 2);
                            oaw.PingJinPing = Convert.ToInt32(analysis.Sum(a => a.PingJinPing) / change.Count());
                            oaw.Profit = Math.Round(change.Sum(a => a.Profit) / change.Count(), 2);
                            oaw.RaiseRate = Math.Round(analysis.Sum(a => a.RaiseRate) / change.Count(), 2);
                            oaw.RiskCount = Convert.ToInt32(analysis.Sum(a => a.RiskCount) / change.Count());
                            oaw.SumLost = Math.Round(change.Sum(a => a.SumLost) / change.Count(), 2);
                            oaw.SumWin = Math.Round(change.Sum(a => a.SumWin) / change.Count(), 2);

                            oaw.UsedMargin = Math.Round(change.Sum(a => a.UsedMargin) / change.Count(), 2);
                            oaw.Utilization = Math.Round(analysis.Sum(a => a.Utilization) / change.Count(), 2);
                            oaw.WinCount = Convert.ToInt32(change.Sum(a => a.WinCount) / change.Count());
                            oaw.WinRate = Math.Round(analysis.Sum(a => a.WinRate) / change.Count(), 2);
                            oaw.Yield = Math.Round(analysis.Sum(a => a.Yield) / change.Count(), 2);
                            db.OperatorAvgWeek.Add(oaw);
                        }
                    }
                    else
                    {
                        if (change.Count > 0)
                        {
                            exist.First().AvgBillBenefit = Math.Round(analysis.Sum(a => a.AvgBillBenefit) / change.Count(), 2);
                            exist.First().AvgPostionTime = Math.Round(analysis.Sum(a => a.AvgPostionTime) / change.Count(), 2);
                            exist.First().BillCount = Math.Round(change.Sum(a => a.BillCount) / change.Count(), 2);
                            exist.First().DayCount = Math.Round(change.Sum(a => a.DayCount) / change.Count(), 2);
                            exist.First().Free = Math.Round(change.Sum(a => a.Free) / change.Count(), 2);
                            exist.First().LoseCount = Convert.ToInt32(change.Sum(a => a.LoseCount) / change.Count());
                            exist.First().MaxReturn = Math.Round(analysis.Sum(a => a.MaxReturn) / change.Count(), 2);
                            exist.First().OperatorId = op.Id;
                            exist.First().OverStopLoss = Math.Round(analysis.Sum(a => a.OverStopLoss) / change.Count(), 2);
                            exist.First().PingJinPing = Convert.ToInt32(analysis.Sum(a => a.PingJinPing) / change.Count());
                            exist.First().Profit = Math.Round(change.Sum(a => a.Profit) / change.Count(), 2);
                            exist.First().RaiseRate = Math.Round(analysis.Sum(a => a.RaiseRate) / change.Count(), 2);
                            exist.First().RiskCount = Convert.ToInt32(analysis.Sum(a => a.RiskCount) / change.Count());
                            exist.First().SumLost = Math.Round(change.Sum(a => a.SumLost) / change.Count(), 2);
                            exist.First().SumWin = Math.Round(change.Sum(a => a.SumWin) / change.Count(), 2);

                            exist.First().UsedMargin = Math.Round(change.Sum(a => a.UsedMargin) / change.Count(), 2);
                            exist.First().Utilization = Math.Round(analysis.Sum(a => a.Utilization) / change.Count(), 2);
                            exist.First().WinCount = Convert.ToInt32(change.Sum(a => a.WinCount) / change.Count());
                            exist.First().WinRate = Math.Round(analysis.Sum(a => a.WinRate) / change.Count(), 2);
                            exist.First().Yield = Math.Round(analysis.Sum(a => a.Yield) / change.Count(), 2);
                        }
                    }
                    #region 取均值


                    #endregion
                }
                #endregion


            }
            db.SaveChanges();
        }


        /// <summary>
        /// 获取交易员的周均值
        /// </summary>
        /// <param name="operatorId"></param>
        /// <param name="tradeHistoryId"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public Models.View.OperatorAvgWeek GetOperatorAvgWeek(int operatorId, int year, int week)
        {
            var query = db.OperatorAvgWeek.Where(a => a.OperatorId == operatorId).Where(a => a.Year == year).Where(a => a.Week == week);
            if (query.Count() > 0)
            {
                return query.First();
            }
            else
            {
                return new Models.View.OperatorAvgWeek();
            }
        }


        public void Delete(int tradeHistoryId)
        {
            var th = db.TradeHistorys.Find(tradeHistoryId);
            var query = db.OperatorAvgWeek.Where(a => a.Year == th.YPdate).Where(a => a.Week == th.WPdate);
            foreach (Models.View.OperatorAvgWeek w in query)
            {
                db.OperatorAvgWeek.Remove(w);
            }
            db.SaveChanges();
        }
    }
}
